#include <iostream>
#include <string>

using namespace std;

class Solution {
public:
    int romanToInt(string s)
    {
        int result = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'I' && i + 1 < s.size()) {
                if (s[i + 1] == 'V' || s[i + 1] == 'X') {
                    result += number_convert(s[i + 1]) - 1;
                    i++;
                } else
                    result += 1;
            } else if (s[i] == 'X' && i + 1 < s.size()) {
                if (s[i + 1] == 'L' || s[i + 1] == 'C') {
                    result += number_convert(s[i + 1]) - 10;
                    i++;
                } else
                    result += 10;
            } else if (s[i] == 'C' && i + 1 < s.size()) {
                if (s[i + 1] == 'D' || s[i + 1] == 'M') {
                    result += number_convert(s[i + 1]) - 100;
                    i++;
                } else
                    result += 100;
            } else {
                result += number_convert(s[i]);
            }
        }
        return result;
    }

    int number_convert(char ch)
    {
        switch (ch) {
        case 'I':
            return 1;
        case 'V':
            return 5;
        case 'X':
            return 10;
        case 'L':
            return 50;
        case 'C':
            return 100;
        case 'D':
            return 500;
        case 'M':
            return 1000;
        }
    }
};

int main(int argc, char const* argv[])
{
    Solution temp;
    cout << temp.romanToInt("LVIII") << endl;
    cout << temp.romanToInt("MCMXCIV") << endl;
    return 0;
}